<template>
  <div>
    <el-container>
      <el-header height="70px">
        <p class="header_title">商品类型管理</p>
        <!--  -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>商品维护</el-breadcrumb-item>
          <el-breadcrumb-item>商品类型管理</el-breadcrumb-item>
          <el-breadcrumb-item></el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>

      <el-main>
        <el-card class="box-card">
          <div slot="header">
            <span>商品类型列表</span>
          </div>

          <el-row :gutter="40">
            <el-col :span="7">
              <el-button
                type="success"
                class="add"
                @click="addDialogVisible = true"
                >添加</el-button
              >
            </el-col>
            <el-col :span="8">
              <el-input placeholder="根据名称查询" clearable>
                <el-button slot="append" icon="el-icon-search"></el-button>
              </el-input>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" class="candelar" @click="enterCalendar"
                >商品日历</el-button
              >
            </el-col>
          </el-row>

          <!-- 表格 -->
          <el-table :data="pagelist" style="width: 100%" border stripe>
            <el-table-column prop="name" label="商品名" width="100px">
            </el-table-column>
            <el-table-column prop="type_name" label="商品类型" width="80px">
            </el-table-column>
            <el-table-column prop="image" label="图片" width="60px">
              <template slot-scope="scope">
                <el-image
                  :src="scope.row.image"
                  style="width:50px;height:50px"
                  fit="cover"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column prop="price" label="价格" width="80px">
            </el-table-column>
            <el-table-column prop="area" label="地区"></el-table-column>
            <el-table-column
              prop="display"
              label="是否首页显示"
              width="80px"
              :formatter="displayFormate"
            >
            </el-table-column>
            <el-table-column
              prop="topping"
              label="是否置顶"
              width="80px"
              :formatter="topFormate"
            >
            </el-table-column>
            <el-table-column prop="sequence" label="置顶顺序" width="80px">
            </el-table-column>
            <el-table-column
              prop="date"
              label="创建时间"
              width="160px"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini">编辑</el-button>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.pagenum"
            :page-sizes="[3, 5, 10, 20]"
            :page-size="pagination.pagesize"
            layout="total,sizes,prev, pager, next"
            :total="pagination.total"
          >
          </el-pagination>
        </el-card>
      </el-main>
    </el-container>

    <!-- 添加商品列表 -->
    <!-- 添加对话框 -->
    <el-dialog
      title="添加行为信息"
      :visible.sync="addDialogVisible"
      width="60%"
      @close="addDialogClosed">
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-row :gutter="20">
          <el-col :span="13">
            <el-form-item label="商品名" prop="name">
              <el-input v-model="addForm.name" type="text"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="商品类型" prop="typeid">
              <el-select v-model="addForm.typeid" placeholder="请选择路径">
                <el-option
                  v-for="item in goodslist"
                  :key="item.id"
                  :label="item.typeName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="是否显示" prop="display">
              <el-switch
                v-model="addForm.display"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="置顶" prop="topping">
              <el-switch
                v-model="addForm.topping"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item prop="area" label="地区">
              <el-select
                v-model="addForm.area"
                placeholder="请选择地区"
                style="width: 100%"
              >
                <el-option
                  v-for="item in stateArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item prop="file">
          <div class="pictitle">
            <b>上传头像</b>(请上传文件大小不超过10MB的正方形图片)
          </div>
          <!-- 头像文件上传 -->
          <el-upload
            class="avatar-uploader"
            ref="upload"
            action="#"
            :show-file-list="false"
            :multiple="false"
            :auto-upload="false"
            :on-change="imgSaveToUrl"
          >
            <img v-if="this.addForm.image" :src="this.addForm.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item prop="unit" label="商品单位">
          <el-input v-model="addForm.unit" type="text"></el-input>
        </el-form-item>
        <el-form-item prop="tid">
          <el-input v-model="tid" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
export default {
  data() {
    return {
      manageInfo: {
        products: "",
        total: 2,
      },
      pagination: {
        pagenum: 1,
        pagesize: 2,
        total: 2,
      },
      pagelist: [],
      addDialogVisible: false,
      addForm: {
        area: "",
        display: "",
        image:"",
        name: "",
        introduction: "",
        sequence: "",
        topping: "",
        typeid: "",
        unit: "",
      },
       file: "",
       tid:"",
      addFormRules: {
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
      },
      stateArr: [
        { value: "gz", label: "广州" },
        { value: "fs", label: "佛山" },
        { value: "sz", label: "深圳" },
        { value: "dg", label: "东莞" },
      ],
      stateArr1: [
        { value: "1", label: "是" },
        { value: "0", label: "否" },
      ],
      goodsInfo: {
        product_types: "",
        total: 2,
      },
      goodslist: [],
      pagination1: {
        pagenum: 1,
        pagesize: 5,
        total: 4,
      },
    };
  },
  methods: {
    async getManageList() {
      const res = await this.http.get("/product/select", this.pagination);
      this.pagelist = res.products;
      this.pagination.total = res.total;
    },
    handleSizeChange(newSize) {
      this.pagination.pagesize = newSize;
      this.getManageList();
    },
    handleCurrentChange(newPage) {
      this.pagination.pagenum = newPage;
      this.getManageList();
    },
    enterCalendar() {
      this.$router.push("/calendar");
    },
    // 监听增添行为对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    onSubmit() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post("/product/add", this.addForm);

        if (status.code !== 200 ) {
          this.$message.error("添加行为失败");
        }
        this.$message.success("添加行为成功");
        this.addDialogVisible = false;
        this.getManageList();
      });
    },
    //图片上传
    async imgSaveToUrl(event) {
      if (this.beforeUploadPicture(event.raw)) {
        var formData = new FormData();
        formData.append("file", event.raw);
        console.log(formData);
        this.http.upload("/product/upload", formData).then((res) => {
          if (res.code == 200) {
            this.addForm.image = res.data
            console.log(res);
            this.$message.success("上传成功");
          }
        });
      }
    },
    // 图片上传之前
    beforeUploadPicture(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG 或者 PNG 格式!");
      }
      if (isJPG && isPNG) {
        if (!isLt10M) {
          this.$message.error("上传头像图片大小不能超过 10MB!");
        }
      }

      return (isJPG || isPNG) && isLt10M;
    },
    // el-select获取商品类型表单的商品类型名称
    async getGoodsList() {
      const res = await this.http.get("/type/select", this.pagination1);
      console.log(res);
      this.goodslist = res.product_types;
      this.pagination.total = res.total;
      console.log(res);
    },
    // 是否置顶
    topFormate(row, index) {
      if (row.topping == 1) {
        return "是";
      } else {
        return "否";
      }
    },
    //是否显示
    displayFormate(row, index) {
      if (row.display == 1) {
        return "是";
      } else {
        return "否";
      }
    },
  },
  created() {
    this.getManageList();
    this.getGoodsList();
  },
};
</script>

<style scoped>
.el-table {
  font-size: 10px;
}
.candelar {
  margin-left: 250px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 2px dashed #dedede;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
